package org.abettor.pushbox.activity2;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.abettor.pushbox.R;
import org.abettor.pushbox.activity.OptionActivity;
import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.download.DownloadMsg;
import org.abettor.pushbox.download.ParaDownloadMsg;
import org.abettor.pushbox.model.LastTimeIdInterface;
import org.abettor.pushbox.model.MessageBean;
import org.abettor.pushbox.model.NotifyBean;
import org.abettor.pushbox.model.SelfMessage;

import android.app.AlertDialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 站内消息的activity
 * @author jzh
 *
 */
public class SiteMessageActivity extends AbstractSendListActivity{
	private ProgressDialog progressDialog;// 进度对话框
	private DownloadMsg down = null;
	private int boxId = -1;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		SelfMessage self = Config.readSelfMessage(this);
		if (self == null) {
			super.superOncreate(savedInstanceState);
			alertMsg();
			return;
		}
		down = new DownloadMsg(this);
		super.onCreate(savedInstanceState);
		setTitle(R.string.label_message);
		int notifyId = getIntent().getIntExtra(NotifyBean.NOTIFY_TAG, -1);
		if(notifyId >= 0)
		{
			NotificationManager notiMgmt = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			notiMgmt.cancel(NotifyBean.NOTIFY_TAG, notifyId);
		}

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		boxId = extras.getInt("_id");
		//加载数据
		loadNewstTList();
	}
	// 更新消息的操作
	private void alertMsg() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		AlertDialog dialog = builder.create();
		dialog.setTitle(R.string.msg);
		dialog.setMessage(this.getText(R.string.input_nickname_first));
		// 拦截back key
		dialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					Intent in = new Intent();
					in.setClass(SiteMessageActivity.this,
							OptionActivity.class);
					startActivity(in);
					SiteMessageActivity.this.finish();
					return true;
				}
				return false;
			}
		});
		dialog.setButton(DialogInterface.BUTTON_POSITIVE,
				this.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent in = new Intent();
						in.setClass(SiteMessageActivity.this,
								OptionActivity.class);
						startActivity(in);
						SiteMessageActivity.this.finish();
					}
				});
		dialog.show();
	}
	@Override
	protected int isHasMoreResult(String content) {
		return ParaDownloadMsg.isHasMoreMsg(content);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected List pareMoreResult(String content) {
		return ParaDownloadMsg.paraMessage(content);
	}

	@Override
	protected String loadEarlyStrFromNet(Date date) {
		SelfMessage self = Config.readSelfMessage(this);
		return down.downEarlyMsgStr(self.getNickName(), boxId, date, Config.PAGESIZE);
	}

	@Override
	protected String loadOldStrFromNet(Date date) {
		SelfMessage self = Config.readSelfMessage(this);
		return down.downNewMsgStr(self.getNickName(), boxId, date, Config.PAGESIZE);
	}

	@Override
	protected void showOldProgressBar() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				progressDialog = new ProgressDialog(SiteMessageActivity.this);
				progressDialog
						.setTitle(R.string.abstract_sendlist_activity_loadmsg);
				progressDialog.setMessage(SiteMessageActivity.this
						.getText(R.string.abstract_sendlist_activity_loadnetdata_loading));
				progressDialog.show();
			}
		});
	}

	@Override
	protected void dimissOldProgressBar() {
		progressDialog.dismiss();
	}

	@Override
	protected View getListView(int position, View convertView,
			ViewGroup parent, LastTimeIdInterface bean) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		MessageBean item = (MessageBean) bean;
		LayoutInflater inflater = SiteMessageActivity.this.getLayoutInflater();
		ViewGroup theParent = (ViewGroup) inflater.inflate(
				R.layout.site_message_activity_item, parent, false);
		TextView title = (TextView) theParent
				.findViewById(R.id.msg_nick);
		title.setText(changeMsgToStrTitle(item));
		TextView content = (TextView) theParent
				.findViewById(R.id.msg_content);
		content.setText(item.getContent());
		TextView updateTime = (TextView) theParent
				.findViewById(R.id.update_time);
		updateTime.setText(sdf.format(item.getTime()));
		return theParent;
	}
	private String changeMsgToStrTitle(MessageBean item) {
		String str = "";
		str = str + item.getNickName();
		SelfMessage self = Config.readSelfMessage(this);
		if(self.getNickName().equals(item.getNickName())){
			str = str + "("+this.getText(R.string.message_board_activity_me)+")";
		}
		return str;
	}
	@Override
	protected boolean sendMsgOperation(String msg) {
		return down.sendMsg(boxId, msg);
	}

}
